package com.fulihui.wgj.biz.impl;


import com.fulihui.common.enums.ContainOrEliminateEnum;
import com.fulihui.common.util.DateUtils;
import com.fulihui.common.util.ObjectFieldUtil;
import com.fulihui.wgj.biz.WxUserInfoBiz;
import com.fulihui.wgj.dal.impala.domain.WxUserInfoSend;
import com.fulihui.wgj.dal.impala.mapper.UserInfoMapper;
import com.fulihui.wgj.facade.request.game.CalcSendCountGameContainRequest;
import com.fulihui.wgj.facade.request.game.CalcSendCountGameEliminateRequest;
import com.fulihui.wgj.facade.request.template.CalcSendCountRequest;
import com.fulihui.wgj.facade.request.youzan.TradeOrderQueryRequest;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.util.CollectionUtils;

import java.text.ParseException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.Iterator;
import java.util.List;
import java.util.Objects;

import static org.apache.commons.lang3.StringUtils.isNotBlank;
import static org.apache.commons.lang3.StringUtils.isNotEmpty;

/**
 * 微信用户相关Biz
 *
 * @author wang_wx
 * @date 2018-07-24
 */
@Component
public class WxUserInfoBizImpl implements WxUserInfoBiz {

    private static final Logger LOGGER = LoggerFactory.getLogger(WxUserInfoBizImpl.class);

    @Autowired
    UserInfoMapper userInfoMapper;

    @Override
    public Long queryCount(CalcSendCountRequest request) {
        return userInfoMapper.countSendUser(request.getAppId(), builderQuery(request));
    }

    @Override
    public List<WxUserInfoSend> queryPage(CalcSendCountRequest request, String nextOpenid) {
        String query = builderQuery(request);
        StringBuilder builder = new StringBuilder(query);
        if (StringUtils.isBlank(nextOpenid)) {
            builder.append(" ORDER BY openid LIMIT ").append(request.getRows());
        } else {
            builder.append(" AND openid > ").append(nextOpenid).append(" ORDER BY openid LIMIT ").append(request.getRows());
        }
        List<WxUserInfoSend> wxUserInfos = userInfoMapper.querySendUser(request.getAppId(), builder.toString());
        return wxUserInfos;
    }

    @Override
    public Long queryCount4Game(CalcSendCountRequest request) {
        return userInfoMapper.count4Game(request.getAppId(), builderQuery4Game(request));
    }

    @Override
    public List<WxUserInfoSend> queryPage4Game(CalcSendCountRequest request, String nextOpenid) {
        String query = builderQuery4Game(request);
        StringBuilder builder = new StringBuilder(query);
        if (StringUtils.isBlank(nextOpenid)) {
            builder.append(" ORDER BY a.openid LIMIT ").append(request.getRows());
        } else {
            builder.append(" AND a.openid > ").append(nextOpenid).append(" ORDER BY a.openid LIMIT ").append(request.getRows());
        }
        List<WxUserInfoSend> wxUserInfos = userInfoMapper.querySendUser4Game(request.getAppId(), builder.toString());
        return wxUserInfos;
    }

    /**
     * 组装查询条件-电商
     *
     * @param request
     * @return
     */
    private String builderQuery(CalcSendCountRequest request) {
        StringBuffer column = new StringBuffer(" AS utab ");

        // 有赞过滤条件
        if (Objects.nonNull(request.getYzFilter())) {
            String yzQuery = buildYZQuery(request.getYzFilter());
            //left join联查
            column.append(" INNER JOIN (" + yzQuery + ")  AS yztab ON utab.openid= yztab.open_id ");
        }

        column.append(" WHERE 1=1 ");

        //region 国家,如果为空，那么省份城市也为空
        if (isNotBlank(request.getCountry())) {
            if (StringUtils.equals(request.getCountryTab(), ContainOrEliminateEnum.CONTAIN.getCode())) {
                column.append(" AND utab.country IN ").append(" ( ").append(list2String(request.getCountry().split(","))).append(") ");
            } else {
                //如果选择了排除国家，但是却选择了下一级城市并且是排除的状态，才需要not in国家
                if (StringUtils.isBlank(request.getProvince())
                        || StringUtils.equals(request.getProvinceTab(), ContainOrEliminateEnum.ELIMINATE.getCode())) {
                    column.append(" AND utab.country NOT IN ").append(" ( ").append(list2String(request.getCountry().split(","))).append(") ");
                }
            }

            //省份
            if (isNotEmpty(request.getProvince())) {
                if (StringUtils.equals(request.getProvinceTab(), ContainOrEliminateEnum.CONTAIN.getCode())) {
                    column.append(" AND utab.province IN ").append(" ( ").append(list2String(request.getProvince().split(","))).append(") ");
                } else {
                    column.append(" AND utab.province NOT IN ").append(" ( ").append(list2String(request.getProvince().split(","))).append(") ");
                }
            }

            //城市
            if (isNotEmpty(request.getCity())) {
                if (StringUtils.equals(request.getCityTab(), ContainOrEliminateEnum.CONTAIN.getCode())) {
                    column.append(" AND utab.city IN ").append(" ( ").append(list2String(request.getCity().split(","))).append(") ");
                } else {
                    column.append(" AND utab.city NOT IN ").append(" ( ").append(list2String(request.getCity().split(","))).append(") ");
                }
            }
        }
        //endregion

        //region 点击条件过滤

        //粉丝性别
        if (Objects.nonNull(request.getSex())) {
            if (request.getSexTab().equals(ContainOrEliminateEnum.CONTAIN.getCode())) {
                column.append(" AND utab.sex = ").append(request.getSex());
            } else {
                column.append(" AND utab.sex != ").append(request.getSex());
            }
        }

        // 操作系统
        if (StringUtils.isNotBlank(request.getOs())) {
            if (request.getOsTab().equals(ContainOrEliminateEnum.CONTAIN.getCode())) {
                column.append(" AND utab.os = '").append(request.getOs()).append("'");
            } else {
                column.append(" AND utab.os != '").append(request.getOs()).append("'");
            }
        }

        //是否退订
        if (Objects.nonNull(request.getIsTd())) {
            if (request.getIsTdTab().equals(ContainOrEliminateEnum.CONTAIN.getCode())) {
                column.append(" AND utab.is_td = ").append(request.getIsTd());
            } else {
                column.append(" AND ( utab.is_td != ").append(request.getIsTd()).append(" OR utab.is_td is NULL )");
            }
        }

        //是否点击过链接
        if (isNotBlank(request.getHasClickLink())) {
            if (request.getHasClickLinkTab().equals(ContainOrEliminateEnum.CONTAIN.getCode())) {
                column.append(" AND utab.has_click_link = ").append(request.getHasClickLink());
            } else {
                column.append(" AND ( utab.has_click_link != ").append(request.getHasClickLink()).append(" OR utab.has_click_link is NULL )");
            }
        }

        //是否点击过小程序
        if (isNotBlank(request.getHasClickMiniProgram())) {
            if (request.getHasClickMiniProgramTab().equals(ContainOrEliminateEnum.CONTAIN.getCode())) {
                column.append(" AND utab.has_click_mini_program = ").append(request.getHasClickMiniProgram());
            } else {
                column.append(" AND utab.has_click_mini_program != ").append(request.getHasClickMiniProgram());
            }
        }


        //粉丝分组
        if (isNotEmpty(request.getGroupid())) {
            if (request.getGroupTab().equals(ContainOrEliminateEnum.CONTAIN.getCode())) {
                column.append(" AND utab.groupid = ").append(request.getGroupid());
            } else {
                column.append(" AND utab.groupid != ").append(request.getGroupid());
            }
        }

        //粉丝标签
        if (isNotEmpty(request.getTag())) {
            if (request.getTagTab().equals(ContainOrEliminateEnum.CONTAIN.getCode())) {
                column.append(" AND utab.tag LIKE '%").append(request.getTag()).append("%'");
            } else {
                column.append(" AND utab.tag NOT LIKE '%").append(request.getTag()).append("%'");
            }
        }

        //二维码扫描场景
        if (isNotEmpty(request.getQrSceneStr())) {
            if (StringUtils.equals(request.getQrSceneStrTab(), ContainOrEliminateEnum.CONTAIN.getCode())) {
                column.append(" AND utab.qr_scene_str IN ").append(" ( ").append(list2String(request.getQrSceneStr().split(","))).append(") ");
            } else {
                column.append(" AND utab.qr_scene_str NOT IN ").append(" ( ").append(list2String(request.getQrSceneStr().split(","))).append(") ");
            }
        }

        //endregion


        //region 用户过滤条件

        //注册时间，毫秒数
        Date subStartTime = request.getSubStartTime();
        Date subEndTime = request.getSubEndTime();

        if (Objects.nonNull(subStartTime)) {
            column.append(" AND utab.subscribe_time >= ").append(subStartTime.getTime());
        }
        if (Objects.nonNull(subEndTime)) {
            column.append(" AND utab.subscribe_time < ").append(subEndTime.getTime());
        }

        //endregion

        LOGGER.info("组合的sql：{}", column.toString());

        return column.toString();
    }

    /**
     * 组装查询条件-游戏
     *
     * @param request
     * @return
     */
    private String builderQuery4Game(CalcSendCountRequest request) {
        StringBuffer condition = new StringBuffer(" AS a");
        CalcSendCountGameContainRequest countGameContainRequest = request.getContainRequest();
        CalcSendCountGameEliminateRequest countGameEliminateRequest = request.getEliminateRequest();
        // 有游戏条件
        if (!ObjectFieldUtil.isAllFieldEmpty(countGameContainRequest) || !ObjectFieldUtil.isAllFieldEmpty(countGameEliminateRequest)) {
            condition.append(" LEFT OUTER JOIN user_game_behavior AS b ON a.openid = b.open_id");
        }
        condition.append(" WHERE 1=1");
        //国家,如果为空，那么省份城市也为空
        if (isNotEmpty(request.getCountry())) {
            if (StringUtils.equals(request.getCountryTab(), ContainOrEliminateEnum.CONTAIN.getCode())) {
                condition.append(" AND a.country IN ").append(" ( ").append(list2String(request.getCountry().split(","))).append(") ");
            } else {
                //如果选择了排除国家，但是却选择了下一级城市并且是排除的状态，才需要not in国家
                if (StringUtils.isBlank(request.getProvince())
                        || StringUtils.equals(request.getProvinceTab(), ContainOrEliminateEnum.ELIMINATE.getCode())) {
                    condition.append(" AND a.country NOT IN ").append(" ( ").append(list2String(request.getCountry().split(","))).append(") ");
                }
            }

            //省份
            if (isNotEmpty(request.getProvince())) {
                if (StringUtils.equals(request.getProvinceTab(), ContainOrEliminateEnum.CONTAIN.getCode())) {
                    condition.append(" AND a.province IN ").append(" ( ").append(list2String(request.getProvince().split(","))).append(") ");
                } else {
                    condition.append(" AND a.province NOT IN ").append(" ( ").append(list2String(request.getProvince().split(","))).append(") ");
                }
            }

            //城市
            if (isNotEmpty(request.getCity())) {
                if (StringUtils.equals(request.getCityTab(), ContainOrEliminateEnum.CONTAIN.getCode())) {
                    condition.append(" AND a.city IN ").append(" ( ").append(list2String(request.getCity().split(","))).append(") ");
                } else {
                    condition.append(" AND a.city NOT IN ").append(" ( ").append(list2String(request.getCity().split(","))).append(") ");
                }
            }
        }

        //粉丝性别
        if (Objects.nonNull(request.getSex())) {
            if (request.getSexTab().equals(ContainOrEliminateEnum.CONTAIN.getCode())) {
                condition.append(" AND a.sex = ").append(request.getSex());
            } else {
                condition.append(" AND a.sex != ").append(request.getSex());
            }
        }

        //操作系统
        if (isNotEmpty(request.getOs())) {
            if (request.getOsTab().equals(ContainOrEliminateEnum.CONTAIN.getCode())) {
                condition.append(" AND a.os = '").append(request.getOs()).append("'");
            } else {
                condition.append(" AND a.os != '").append(request.getOs()).append("'");
            }
        }

        //是否退订
        if (Objects.nonNull(request.getIsTd())) {
            if (request.getIsTdTab().equals(ContainOrEliminateEnum.CONTAIN.getCode())) {
                condition.append(" AND a.is_td = ").append(request.getIsTd());
            } else {
                condition.append(" AND ( a.is_td != ").append(request.getIsTd()).append(" OR a.is_td is NULL )");
            }
        }

        //是否点击过链接
        if (isNotBlank(request.getHasClickLink())) {
            if (request.getHasClickLinkTab().equals(ContainOrEliminateEnum.CONTAIN.getCode())) {
                condition.append(" AND a.has_click_link = ").append(request.getHasClickLink());
            } else {
                condition.append(" AND ( a.has_click_link != ").append(request.getHasClickLink()).append(" OR a.has_click_link is NULL )");
            }
        }

        //是否点击过小程序
        if (isNotBlank(request.getHasClickMiniProgram())) {
            if (request.getHasClickMiniProgramTab().equals(ContainOrEliminateEnum.CONTAIN.getCode())) {
                condition.append(" AND a.has_click_mini_program = ").append(request.getHasClickMiniProgram());
            } else {
                condition.append(" AND a.has_click_mini_program != ").append(request.getHasClickMiniProgram());
            }
        }

        //二维码扫描场景
        if (isNotEmpty(request.getQrSceneStr())) {
            if (StringUtils.equals(request.getQrSceneStrTab(), ContainOrEliminateEnum.CONTAIN.getCode())) {
                condition.append(" AND a.qr_scene_str IN ").append(" ( ").append(list2String(request.getQrSceneStr().split(","))).append(") ");
            } else {
                condition.append(" AND a.qr_scene_str NOT IN ").append(" ( ").append(list2String(request.getQrSceneStr().split(","))).append(") ");
            }
        }

        //TODO 用户是否需要过滤重复

        //注册时间，毫秒数
        Date subStartTime = request.getSubStartTime();
        Date subEndTime = request.getSubEndTime();
        if (Objects.nonNull(subStartTime)) {
            condition.append(" AND a.subscribe_time >= ").append(subStartTime.getTime());
        }
        if (Objects.nonNull(subEndTime)) {
            condition.append(" AND a.subscribe_time < ").append(subEndTime.getTime());
        }


        // 游戏相关条件--包含，左外连接，不包含null值
        CalcSendCountGameContainRequest gameRequestContain = countGameContainRequest;
        String firstTagContain = gameRequestContain.getFirstTag();
        /**
         * 一级标签包含的List 用于判断排除的是否要拼接NOT IN
         */
        List<String> firstTagContainList = new ArrayList<>();
        /**
         * 二级标签包含的List 用于判断排除的是否要拼接NOT IN
         */
        List<String> secondTagContainList = new ArrayList<>();
        String secondTagContain = gameRequestContain.getSecondTag();
        if (!Objects.isNull(gameRequestContain)) {
            //一级标签
            if (isNotEmpty(firstTagContain)) {
                condition.append(" AND b.first_tag IN ").append(" ( ").append(list2String(firstTagContain.split(","))).append(") ");
                firstTagContainList = Arrays.asList(firstTagContain.split(","));
            }
            //二级标签
            if (isNotEmpty(secondTagContain)) {
                condition.append(" AND b.second_tag IN ").append(" ( ").append(list2String(secondTagContain.split(","))).append(") ");
                secondTagContainList = Arrays.asList(secondTagContain.split(","));
            }
            String lastLoginStartTime = gameRequestContain.getLastLoginStartTime();
            String lastLoginEndTime = gameRequestContain.getLastLoginEndTime();
            String createRoleStartTime = gameRequestContain.getCreateRoleStartTime();
            String createRoleEndTime = gameRequestContain.getCreateRoleEndTime();
            // 末次登陆
            if (StringUtils.isNotBlank(lastLoginStartTime)) {
                try {
                    Date lastLoginStart = DateUtils.parseNewFormat(lastLoginStartTime);
                    condition.append(" AND b.login_time >= ").append(lastLoginStart.getTime());
                } catch (ParseException e) {
                    LOGGER.error("时间解析异常");
                }
            }
            if (StringUtils.isNotBlank(lastLoginEndTime)) {
                try {
                    Date lastLoginEnd = DateUtils.parseNewFormat(lastLoginEndTime);
                    condition.append(" AND b.login_time <= ").append(lastLoginEnd.getTime());
                } catch (ParseException e) {
                    LOGGER.error("时间解析异常");
                }
            }
            //创角时间
            if (StringUtils.isNotBlank(createRoleStartTime)) {
                try {
                    Date createRoleStart = DateUtils.parseNewFormat(createRoleStartTime);
                    condition.append(" AND b.create_time >= ").append(createRoleStart.getTime());
                } catch (ParseException e) {
                    LOGGER.error("时间解析异常");
                }
            }
            if (StringUtils.isNotBlank(createRoleEndTime)) {
                try {
                    Date createRoleEnd = DateUtils.parseNewFormat(createRoleEndTime);
                    condition.append(" AND b.create_time <= ").append(createRoleEnd.getTime());
                } catch (ParseException e) {
                    LOGGER.error("时间解析异常");
                }
            }

            /**
             * 包含-充值金额
             */
            Integer consumptionStartContain = gameRequestContain.getConsumptionStartContain();
            Integer consumptionEndContain = gameRequestContain.getConsumptionEndContain();
            if (null != consumptionStartContain) {
                condition.append(" AND b.money >= ").append(consumptionStartContain * 100);
            }
            if (null != consumptionEndContain) {
                condition.append(" AND b.money <= ").append(consumptionEndContain * 100);
            }
        }
        // 游戏相关条件--排除 ，并要注意保留 null 的行
        CalcSendCountGameEliminateRequest gameRequestEliminate = countGameEliminateRequest;
        String firstTagEliminate = gameRequestEliminate.getFirstTag();
        String secondTagEliminate = gameRequestEliminate.getSecondTag();
        if (!Objects.isNull(gameRequestEliminate)) {
            //一级标签
            if (isNotEmpty(firstTagEliminate)) {
                String[] firstTagArr = firstTagEliminate.split(",");
                List<String> firstTagEliminateList = Arrays.asList(firstTagArr);
                // 如果1级标签排除的 不在 包含的条件内 才一级和二级2个都NOT IN
                if (!CollectionUtils.isEmpty(firstTagEliminateList)) {
                    /**
                     * Arrays.asList() 返回的是Arrays的内部类ArrayList， 而不是java.util.ArrayList
                     * Arrays的内部类ArrayList和java.util.ArrayList都是继承AbstractList，
                     * remove、add等方法AbstractList中是默认throw UnsupportedOperationException而且不作任何操作。
                     * java.util.ArrayList重写了这些方法而Arrays的内部类ArrayList没有重写，所以会抛出异常
                     */
                    List<String> arrList = new ArrayList(firstTagEliminateList);
                    Iterator<String> iterator = arrList.iterator();
                    while (iterator.hasNext()) {
                        String next = iterator.next();
                        // 排除1级标签在包含的里面的元素
                        if (firstTagContainList.contains(next)) {
                            iterator.remove();
                        }
                    }
                    firstTagArr = arrList.toArray(new String[arrList.size()]);
                    if (!CollectionUtils.isEmpty(arrList)) {
                        condition.append(" AND ( b.first_tag NOT IN ").append(" ( ").append(list2String(firstTagArr)).append(") or b.first_tag is NULL) ");
                    }
                }
            }
            //二级标签
            if (isNotEmpty(secondTagEliminate)) {

                String[] secondTagArr = secondTagEliminate.split(",");
                List<String> secondTagEliminateList = Arrays.asList(secondTagArr);
                // 如果2级标签排除的 不在 包含的条件内 才2级和消费2个都NOT IN
                if (!CollectionUtils.isEmpty(secondTagEliminateList)) {
                    /**
                     * Arrays.asList() 返回的是Arrays的内部类ArrayList， 而不是java.util.ArrayList
                     * Arrays的内部类ArrayList和java.util.ArrayList都是继承AbstractList，
                     * remove、add等方法AbstractList中是默认throw UnsupportedOperationException而且不作任何操作。
                     * java.util.ArrayList重写了这些方法而Arrays的内部类ArrayList没有重写，所以会抛出异常
                     */
                    List<String> arrList = new ArrayList(secondTagEliminateList);
                    Iterator<String> iterator = arrList.iterator();
                    while (iterator.hasNext()) {
                        String next = iterator.next();
                        // 排除2级标签在包含的里面的元素
                        if (secondTagContainList.contains(next)) {
                            iterator.remove();
                        }
                    }
                    secondTagArr = arrList.toArray(new String[arrList.size()]);
                    if (!CollectionUtils.isEmpty(arrList)) {
                        condition.append(" AND ( b.second_tag NOT IN ").append(" ( ").append(list2String(secondTagArr)).append(") or b.second_tag is NULL) ");
                    }
                }

            }
            String lastLoginStartTime = gameRequestEliminate.getLastLoginStartTime();
            String lastLoginEndTime = gameRequestEliminate.getLastLoginEndTime();
            // 末次登陆--排除
            if (StringUtils.isNotBlank(lastLoginStartTime) && StringUtils.isNotBlank(lastLoginEndTime)) {
                try {
                    Date lastLoginStart = DateUtils.parseNewFormat(lastLoginStartTime);
                    condition.append(" AND ( b.login_time < ").append(lastLoginStart.getTime());
                } catch (ParseException e) {
                    LOGGER.error("时间解析异常");
                }
                try {
                    Date lastLoginEnd = DateUtils.parseNewFormat(lastLoginEndTime);
                    condition.append(" OR b.login_time > ").append(lastLoginEnd.getTime()).append("  OR b.login_time is NULL )");
                } catch (ParseException e) {
                    LOGGER.error("时间解析异常");
                }
            } else if (StringUtils.isBlank(lastLoginStartTime) && StringUtils.isNotBlank(lastLoginEndTime)) {
                try {
                    Date lastLoginEnd = DateUtils.parseNewFormat(lastLoginEndTime);
                    condition.append(" AND ( b.login_time > ").append(lastLoginEnd.getTime()).append(" OR b.login_time is NULL )");
                } catch (ParseException e) {
                    LOGGER.error("时间解析异常");
                }
            } else if (StringUtils.isNotBlank(lastLoginStartTime) && StringUtils.isBlank(lastLoginEndTime)) {
                try {
                    Date lastLoginStart = DateUtils.parseNewFormat(lastLoginStartTime);
                    condition.append(" AND ( b.login_time < ").append(lastLoginStart.getTime()).append(" OR b.login_time is NULL )");
                } catch (ParseException e) {
                    LOGGER.error("时间解析异常");
                }
            }

            /**
             * 排除-充值金额
             */
            Integer consumptionStartEliminate = gameRequestEliminate.getConsumptionStartEliminate();
            Integer consumptionEndEliminate = gameRequestEliminate.getConsumptionEndEliminate();
            if (null != consumptionStartEliminate && null != consumptionEndEliminate) {
                condition.append(" AND ( b.money IS NULL OR b.money < ").append(consumptionStartEliminate * 100)
                        .append(" OR b.money > ").append(consumptionEndEliminate * 100).append(" )");
            } else {
                if (null != consumptionStartEliminate) {
                    condition.append(" AND ( b.money IS NULL OR b.money < ").append(consumptionStartEliminate * 100).append(" )");
                }
                if (null != consumptionEndEliminate) {
                    condition.append(" AND ( b.money IS NULL OR b.money > ").append(consumptionEndEliminate * 100).append(" )");
                }
            }

        }

        LOGGER.info("游戏发送组合的sql：{}", condition.toString());

        return condition.toString();
    }

    /**
     * 构建有赞查询条件
     *
     * @param yzFilter
     * @return
     */
    private String buildYZQuery(TradeOrderQueryRequest yzFilter) {

        //查询有赞openid
        StringBuilder yzBuilder = new StringBuilder(" select DISTINCT open_id from youzan_order_status WHERE 1=1 ");

        //有赞商品
        if (yzFilter.getCommodityIds() != null && !yzFilter.getCommodityIds().isEmpty()) {
            yzBuilder.append(" AND item_id in( ");
            yzFilter.getCommodityIds().forEach((item) -> {
                yzBuilder.append(item + ",");
            });
            //删除最后一个，字符串
            yzBuilder.delete(yzBuilder.length() - 1, yzBuilder.length());
            yzBuilder.append(")");
        }

        //订单开始时间
        if (yzFilter.getStartTime() != null) {
            yzBuilder.append(" AND update_time >" + yzFilter.getStartTime());
        }

        //订单结束时间
        if (yzFilter.getEndTime() != null) {
            yzBuilder.append(" AND update_time <" + yzFilter.getEndTime());
        }

        //订单状态
        if (StringUtils.isNotEmpty(yzFilter.getOrderState())) {
            yzBuilder.append(" AND status ='" + yzFilter.getOrderState() + "'");
        }

        return yzBuilder.toString();
    }

    /**
     * 数组转字符串
     *
     * @param arr
     * @return
     */
    private String list2String(String[] arr) {
        StringBuffer str = new StringBuffer();
        for (String s : arr) {
            str.append("\"").append(s).append("\"").append(",");
        }
        return str.substring(0, str.length() - 1);
    }
}